import { ENUM_RES, ENUM_UI } from "./GbzEnum";
import GbzInstance from "./GbzInstance";
import GbzAudioManager from "./manager/GbzAudioManager";
import GbzDataManager from "./manager/GbzDataManager";
import GbzResManager from "./manager/GbzResManager";
import GbzSdkManager from "./manager/GbzSdkManager";


const { ccclass, property } = cc._decorator;

@ccclass
export default class GbzIndex extends cc.Component {

    onLoad() {
        cc.view.setResizeCallback(() => this.setScreenReponsive())
        this.setScreenReponsive()
        GbzDataManager.ins.loading_rate = 0
        const phyMgr = cc.director.getPhysicsManager();
        const colMgr = cc.director.getCollisionManager();
        phyMgr.enabled = true;
        colMgr.enabled = true;
        cc.macro.ENABLE_MULTI_TOUCH = false
    }

    async start() {
        for (const index in ENUM_RES) {
            const obj = ENUM_RES[index]
            await GbzResManager.ins.load(obj)
        }
        GbzInstance.uiMgr.init()
        GbzDataManager.ins.restore()
        GbzAudioManager.ins.play()
        GbzSdkManager.ins.init()
        GbzInstance.uiMgr.toggle(ENUM_UI.LAYER_MENU, true, () => {
            GbzDataManager.ins.loading_rate = 1
        })
    }

    setScreenReponsive() {
        const resolutionSize = cc.view.getDesignResolutionSize();
        const frameSize = cc.view.getFrameSize();
        const setFitWidth = () => {
            cc.Canvas.instance.fitHeight = false;
            cc.Canvas.instance.fitWidth = true;
        }
        const setFitHeight = () => {
            cc.Canvas.instance.fitHeight = true;
            cc.Canvas.instance.fitWidth = false;
        }
        const setFitBoth = () => {
            cc.Canvas.instance.fitHeight = true;
            cc.Canvas.instance.fitWidth = true;
        }
        const resolution = resolutionSize.width / resolutionSize.height
        const frame = frameSize.width / frameSize.height
        if (resolution < 1) {
            if (frame < 1) {
                if (frame > resolution) {
                    setFitBoth()
                } else {
                    setFitWidth()
                }
            } else {
                setFitBoth()
            }
        } else {
            if (frame > 1) {
                if (frame < resolution) {
                    setFitBoth()
                } else {
                    setFitHeight()
                }
            } else {
                setFitBoth()
            }
        }
    }
}
